<?php

class Preview extends MY_Controller {

	function __construct()
	{
		
		
		parent::__construct();
		$this->page=$this->module_codes['Preview'];
		$this->load->model('preview_model' , 'PRM');
	}
	
	function index($data=array())
	{
		//collect all list prices to display them on the preview page
		$this->check_page_permission('access');
		$this->load->model('list_price_model' , 'LPM');
		$result = $this->LPM->get_list_price();
		if($result['count'] == 0){
			$data['count'] = 0;
		}
		else{
			$data['count'] = $result['count'];
			$data['list_price'] = $result['info'];
		}
		
		//collect all preview list to display them on the preview page
		$result = $this->PRM->get_list_preview();
		if($result['count'] == 0){
			$data['count'] = 0;
		}
		else{
			$data['count'] = $result['count'];
			$data['list_preview'] = $result['info'];
		}


                //collect all templates
                $result = $this->PRM->get_template();
                if($result['count'] == 0){
			$data['count_templates'] = 0;
		}
		else{
			$data['count_templates'] = $result['count'];
			$data['templates'] = $result['info'];
		}
                /*
                echo '<pre>';
	 	print_r($data);
		echo '</pre>';
                */
		
		$this->load->view('head');
		$this->load->view('preview/main' , $data);
		$this->load->view('footer');
	}
	  function new_preview($patient_id=NULL){
	  	$this->check_page_permission('add');
	  	$data['patient_id']=$patient_id;
	    $this->load->model('patient_model' , 'PM');
		$result = $this->PM->get_patient_data($patient_id);
		if(!empty($result)){
			$data['patient_name']= $result['first_name'].' '.$result['last_name'];
			$data['code']= $result['code'];
		}  
		$this->index($data);
	  	
	  }

        function __collect_array($array)
        {
            $var = '';
            if(is_array($array))
            foreach ($array as $value) {
                if( $var =='')
                    $var = $value;
                else
                    $var = $var.','.$value;
            }
            
            return $var;
        }
       
        function save_new_preview($id=NULL)
        {
        	$this->check_page_permission('add');
            $patient_id        = $this->input->post('patient_id');
            if($patient_id==NULL)
                return $this->index();
            $this->load->model('patient_history_model' , 'PHM');

            
            $examinations       = $this->input->post('examination_ids');
            $checkups           = $this->input->post('checkup_ids');

          // $checkup_ids     = $this->__collect_array($checkups);
          // $examination_ids = $this->__collect_array($examinations);
           $user_id = 14;
           //$preview_date = date('Y-m-d');
           $note =  $this->input->post('notes');

           $my_array = array(
               'patient_id'         => $patient_id,
               'user_id'            => $user_id,
               'note'               => $note
           );

           /*
            echo "<pre>";
            print_r($my_array);
            echo "</pre>";
            */

           if($this->PHM->insert_history($my_array,$checkups,$examinations))
               redirect('preview/' , 'refresh');
        }
	
	function setting_preview()
	{
		$this->check_page_permission('access');
		//get all preview(s) to display them
		$result = $this->PRM->get_list_preview();
		
		//if $result['count'] = 0 then there is no data
		//else generate array for all types
		if($result['count'] == 0){
			$data['count'] = 0;
		}
		else{
			$data['count'] = $result['count'];
			$data['result'] = $result['info'];
		}

		$this->load->view('head');
		$this->load->view('preview/setting_main', $data);
		$this->load->view('footer');
	}
	
	function setting_form($id=NULL)
	{
		$this->check_page_permission('access');
		$preview_info	= $id ? $this->PRM->get_list_preview($id) : array();

		$this->load->view('head');
		$this->load->view('preview/setting_form', $preview_info);
		$this->load->view('footer');
	}

        function setting_form2($id=NULL)
        {
        	
            $form_data = $this->input->post('preview_form');
           // $form_data['id'] = $id;
            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            //$data = array();
            if($this->input->post('submit'))
            {
                if ($this->form_validation->run('preview_setting_form') == FALSE)
                {
                    return $this->setting_form();
                }
                else
                {
                    //Case 1: if ID is NOT set then insert new
                    if(isset($form_data['id']) && !empty($form_data['id'])){
                    	$this->check_page_permission('edit');
                        if($this->PRM->update_list_preview_by_id($form_data, $form_data['id']))
                            redirect('preview/setting_preview' , 'refresh');
                    }
                    else{
                    	$this->check_page_permission('add');
                        //Case 2: if ID is  set then update if($this->PRM->insert_preview($form_data))
                         if($this->PRM->insert_preview($form_data))
                           redirect('preview/setting_preview' , 'refresh');
                        

                    }

                }
            }
        }

        //this function return the contents of the template based on the id
        // can be used by ajax (like the main page on the preview) or can be used normally
        function ajax_get_template_content($id=NULL)
        {
        	$this->check_page_permission('access');
            if($id){
                $result = $this->PRM->get_template($id);
                if(is_array($result) || !empty($result))
                echo json_encode($result);
            }
        }

        function preview_template($id=NULL)
        {
        	$this->check_page_permission('access');
            $result = $this->PRM->get_template($id);
            
            $this->load->view('head');
            $this->load->view('preview/setting_preview_template', $result);
            $this->load->view('footer');
        }

        function save_template($id=NULL)
        {
            $formData = $this->input->post('template');

            if(!empty($formData['id']))
                {
                 
        	     $this->check_page_permission('edit');
                 if($this->PRM->update_template_by_id($formData, $formData['id']))
                      echo lang('saved_successfully');
                 else
                      echo lang('saved_failed');
                }
            else
                {
                
        	       $this->check_page_permission('add');
                  if($this->PRM->insert_template($formData))
                      echo lang('saved_successfully');
                  else
                      echo lang('saved_failed');
                }

        }
	
}

/* End of file login.php */
/* Location: ./system/application/controllers/preview.php */